package handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import packet.impl.LoginRequestAbstractPacket;
import packet.impl.LoginResponseAbstractPacket;

/**
 * @author Admin
 */
public class LoginRequestHandler extends SimpleChannelInboundHandler<LoginRequestAbstractPacket> {
  @Override
  protected void channelRead0(ChannelHandlerContext ctx, LoginRequestAbstractPacket loginRequestPacket) {
    LoginResponseAbstractPacket loginResponsePacket = new LoginResponseAbstractPacket();

    // 登陆校验
    if (valid(loginRequestPacket)) {
      loginResponsePacket.setSuccess(true);
    } else {
      loginResponsePacket.setSuccess(false);
      loginResponsePacket.setReason("账号密码校验错误");
    }

    ctx.channel().writeAndFlush(loginResponsePacket);
  }

  private boolean valid(LoginRequestAbstractPacket loginRequestPacket) {
    return loginRequestPacket != null;
  }

}
